import { ResConfig, TablesConfig } from "../SysConfig/SysConfig";
import ResManage from "./ResManage";



export class TableCaheMap {
    /**数组存储每行数据 */
    public _tableArray: any[] = [];
    /**Map结构存储每行数据 - 表格第一个字段为Key */
    public _tableMap: Map<string, any> = new Map();
}


const { ccclass, property } = cc._decorator;
@ccclass
export default class TableManage extends cc.Component {

    public static Instance: TableManage = null;

    private m_tablePathArray: any = null;
    private m_tableCaheMap: Map<TablesConfig.TableType, TableCaheMap> = new Map();

    private m_curIndex: number = 0;
    private m_isStart: boolean = false;
    private m_onProgress: Function = null;


    onLoad() {
        if (TableManage.Instance == null) {
            TableManage.Instance = this;
        }
        else {
            this.destroy();
            return;
        }
    }

    /**
     * 点击开始游戏,进入数据解析过程
     * @param isStart    //传入true
     * @param onProgress   //进度函数
     */
    public parseTableData(isStart: boolean, onProgress: Function) {
        this.m_isStart = isStart;
        this.m_onProgress = onProgress;
        let bundle = cc.assetManager.getBundle(ResConfig.TablesBundle.bundleName);
        if (bundle) {
            this.m_tablePathArray = bundle.getDirWithPath("/");
        }
    }

    protected update(dt: number): void {
        if (this.m_isStart && this.m_tablePathArray != null) {
            let file: cc.TextAsset = ResManage.Instance.getAssetFomeCache(ResConfig.TablesBundle, this.m_tablePathArray[this.m_curIndex].path, cc.TextAsset);
            if (this.m_tableCaheMap.has(this.m_curIndex as TablesConfig.TableType) == false) {
                this.m_tableCaheMap.set(this.m_curIndex as TablesConfig.TableType, new TableCaheMap());
            }
            let tableCaheMap = this.m_tableCaheMap.get(this.m_curIndex as TablesConfig.TableType);
            if (file) {
                //按照第二行的属性为字段标识
                //对tableCaheMap进行赋值
                let textArray: string[] = file.text.split('\r\n');
                let flagArray: string[] = textArray[1].split('`');
                let typeArray: string[] = textArray[2].split('`');
                for (let i = 3, j = textArray.length; i < j; i++) {
                    let descObj: any = {};
                    flagArray.forEach((value, index) => {
                        let dataArray: string[] = textArray[i].split('`');
                        if (typeArray[index] == 'number') {
                            descObj[flagArray[index]] = Number(dataArray[index]);
                        }
                        else if (typeArray[index] == 'string') {
                            descObj[flagArray[index]] = dataArray[index];
                        }
                        tableCaheMap._tableMap.set(dataArray[0], descObj)
                    })
                    tableCaheMap._tableArray[i - 3] = descObj;
                }
            }
            else {
                console.warn(this.m_tablePathArray[this.m_curIndex].path + '表格不存在,解析失败')
            }

            //判断是否停止调度器
            this.m_curIndex++;
            if (this.m_onProgress) {
                this.m_onProgress(this.m_curIndex, this.m_tablePathArray.length);
            }
            if (this.m_curIndex >= this.m_tablePathArray.length) {
                this.m_isStart = false;
                this.m_onProgress = null;
                this.m_tablePathArray = null;

                //数据已经解析完毕，可以释放ab资源并删除AB包
                ResManage.Instance.releaseaAllBoundles(ResConfig.TablesBundle);
            }
        }
    }

    //获取idKey数据
    public searchTable(tableType: TablesConfig.TableType, idKey: string): any {
        let tableCaheMap: TableCaheMap = this.m_tableCaheMap.get(tableType);
        if (tableCaheMap == null) {
            return null;
        }

        return tableCaheMap._tableMap.get(idKey);
    }

    //获取全部数据
    public searchTables(tableType: TablesConfig.TableType): any[] {
        let tableCaheMap: TableCaheMap = this.m_tableCaheMap.get(tableType);
        if (tableCaheMap == null) {
            return [];
        }

        return tableCaheMap._tableArray;
    }


}
